package com.xzq.erp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzq.erp.domain.dto.CustomerDTO;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.po.Customer;
import com.xzq.erp.domain.query.CustomerPageQuery;
import com.xzq.erp.domain.vo.CustomerVO;
import com.xzq.erp.enums.ResponseCodeEnum;
import com.xzq.erp.exception.BizException;
import com.xzq.erp.mapper.CustomerMapper;
import com.xzq.erp.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户信息表 服务实现类
 * </p>
 *
 * @author xzq
 * @since 2025-05-13
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Override
    public PageDTO<CustomerVO> getCustomerList(CustomerPageQuery query) {
        PageHelper.startPage(query.getCurrent(), query.getSize());
        List<Customer> list = lambdaQuery()
                .like(StringUtils.isNoneBlank(query.getCustomerName()), Customer::getCustomerName, query.getCustomerName())
                .eq(StringUtils.isNoneBlank(query.getPhone()), Customer::getPhone, query.getPhone())
                .eq(query.getCustomerType() != null, Customer::getCustomerType, query.getCustomerType())
                .eq(query.getGrade() != null, Customer::getGrade, query.getGrade())
                .orderByDesc(Customer::getCreateTime)
                .list();
        PageInfo<Customer> page = new PageInfo<>(list);
        List<Customer> records = page.getList();
        if(CollectionUtils.isEmpty(records)){
            return PageDTO.empty(page.getTotal(),(long)page.getPages());
        }
        List<CustomerVO> customerVOS = BeanUtil.copyToList(records, CustomerVO.class);
        return PageDTO.of(new Page<>(query.getCurrent(),query.getSize(),page.getTotal()),customerVOS);
    }

    @Override
    public void addCustomer(CustomerDTO customerDTO) {
        Customer customer = BeanUtil.copyProperties(customerDTO, Customer.class);
        save(customer);
    }

    @Override
    public void updateCustomer(CustomerDTO customerDTO) {
        if(customerDTO.getId() == null){
            throw new BizException(ResponseCodeEnum.PARAM_NOT_VALID);
        }
        Customer customer = BeanUtil.copyProperties(customerDTO, Customer.class);
        updateById(customer);
    }

    @Override
    public List<CustomerVO> getAllCustomers() {
        List<Customer> customers = this.list();
        return customers.stream()
                .map(customer -> {
                    return CustomerVO.builder()
                            .id(customer.getId())
                            .customerName(customer.getCustomerName())
                            .address(customer.getAddress())
                            .phone(customer.getPhone())
                            .customerType(customer.getCustomerType())
                            .grade(customer.getGrade())
                            .historicalTotal(customer.getHistoricalTotal())
                            .createTime(customer.getCreateTime())
                            .build();
                })
                .collect(Collectors.toList());
    }
}
